package com.sto.stoapp.activity.administrator.hardware;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.manager.WifiAdmin;
import com.sto.stoapp.widget.BaseDialog;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.Timer;
import java.util.TimerTask;

/**
 * wifi测试
 * 
 * @author mgf
 * 
 */
@SuppressLint("HandlerLeak")
public class AdminWifiTestActivity extends StoHomeActivity {

	// wifi状态改变
	private static final int WIFI_STATUS_CHANGED = 0xfa;

	private static final int TIMER_TASK_TIME = 90000;

	private WifiAdmin wifiAdmin;

	private WifiBroad wifiBroad;

	private ProgressDialog pDialog;

	private Timer timer;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == WIFI_STATUS_CHANGED) {
				cancelTimer();
				if (pDialog != null && pDialog.isShowing()) {
					pDialog.dismiss();
				}
				justDialog(msg.obj.toString());
			}
		}

	};

	private void setTimer() {
		cancelTimer();
		timer = new Timer();
		timer.schedule(new TimerTask() {

			@Override
			public void run() {
				Message msg = handler.obtainMessage();
				msg.what = WIFI_STATUS_CHANGED;
				msg.obj = getString(R.string.wifi_scan_failure);
				handler.sendMessage(msg);
			}
		}, TIMER_TASK_TIME);
	}

	private void cancelTimer() {
		if (timer != null) {
			timer.cancel();
			timer = null;
		}
	}



	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_wifi_test);
		this.setTitleBottom(R.string.wlan_test, HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
	}

	private void initWidget() {
		wifiBroad = new WifiBroad();
		IntentFilter filter = new IntentFilter(
				WifiManager.WIFI_STATE_CHANGED_ACTION);
		registerReceiver(wifiBroad, filter);
//		findViewById(R.id.button).setOnClickListener(
//				new ClickFinishActivity(this));
		wifiAdmin = new WifiAdmin(this);
		wifiAdmin.openWifi();
		showDialog();
		setTimer();
		// new BufferThread().start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (wifiBroad != null) {
			unregisterReceiver(wifiBroad);
		}
		handler.removeMessages(WIFI_STATUS_CHANGED);
	}

	class WifiBroad extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction()
					.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
				if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
						WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_ENABLED) {
					unregisterReceiver(wifiBroad);
					wifiBroad = null;
					Message msg = handler.obtainMessage();
					msg.what = WIFI_STATUS_CHANGED;
					msg.obj = getString(R.string.wifi_scan_success);
					handler.sendMessage(msg);
				}
			}
		}

	}

	private void justDialog(String content) {
		final BaseDialog dialog = new BaseDialog(this);
		dialog.setTitles(content);
		dialog.show();
		dialog.getvCanncel().setVisibility(View.GONE);
		dialog.getvMessage().setVisibility(View.GONE);
		dialog.getvPresentation().setVisibility(View.GONE);
		dialog.setOnBackClickListener(new OnBackClickListener() {

			@Override
			public void onBackClickListener(boolean res) {
				dialog.dismiss();
				finish();
			}
		});
	}
	
}
